﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyMovieSystem
{
    public partial class FrmUserInfo : Form
    {
        public FrmUserInfo()
        {
            InitializeComponent();
        }

        //加载
        private void FrmUserInfo_Load(object sender, EventArgs e)
        {
            RoleManage.InitialRole();
            BindData();
        }
        /// <summary>
        /// 临时集合
        /// </summary>
        Dictionary<string, Role> dirRole = new Dictionary<string, Role>();
        
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = new BindingList<Role>(RoleManage.dirRole.Values.ToList());
        }

        #region 增删改查
        //查询
        private void button4_Click(object sender, EventArgs e)
        {
            dirRole.Clear();
            if (textBox4.Text.Trim().Length < 1)
            {
                BindData();
            }
            string name = textBox4.Text.Trim();
            foreach (Role  item in RoleManage.dirRole.Values)
            {
                if (item.Name.IndexOf(name) != -1)
                {
                    dirRole.Add(item.Name,item);
                }
            }
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = new BindingList<Role>(dirRole.Values.ToList());
        }

        //增加
        private void button1_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
            txtName.Text = "";
            txtPwd.Text = "";
            button8.Text = "添加";
            comboBox2.SelectedIndex = 0;
        }

        //修改
        string roleName = null;
        private void button2_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
            button8.Text = "修改";
            roleName = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            txtName.Text = RoleManage.dirRole[roleName].Name;
            txtPwd.Text = RoleManage.dirRole[roleName].Pwd;
        }

        //删除
        private void button3_Click(object sender, EventArgs e)
        {
            roleName = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            DialogResult r = MessageBox.Show("确定永久删除用户名为《"+roleName+"》的账号吗？","提示：",
                MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
            if (r != DialogResult.OK)
                return;
            RoleManage.dirRole.Remove(roleName);
            BindData();
        }
        #endregion 

        #region 确定
        //确定
        private void button8_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Trim().Length < 1 || txtPwd.Text.Trim().Length < 1)
            {
                MessageBox.Show("请填写完整信息！", "提示：");
                return;
            }
            string name = txtName.Text.Trim();
            string pwd = txtPwd.Text.Trim();
            if (button8.Text == "修改")
            {
                RoleManage.dirRole[roleName].Name = name;
                RoleManage.dirRole[roleName].Pwd = pwd;
                MessageBox.Show("修改成功！", "提示：");
                panel1.Visible = false;
                return;
            }
            Role role;
            if (comboBox2.SelectedIndex == 0)
            {
                role = new User(name, pwd);
            }
            else
            {
                role = new Admin(name, pwd);
            }
            try
            {
                RoleManage.dirRole.Add(role.Name, role);
                MessageBox.Show("添加成功！","提示：");
                panel1.Visible = false;
            }
            catch
            {
                MessageBox.Show("该用户名已存在！","提示：");
            }
            BindData();
        }
        #endregion

        #region 取消
        //取消
        private void button5_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
        }

        //设置dgv密码栏显示的内容
        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // 把第2列显示*号，*号的个数和实际数据的长度相同
            if (e.ColumnIndex == 1)
            {
                if (e.Value != null && e.Value.ToString().Length > 0)
                {
                    e.Value = new string('*', e.Value.ToString().Length);
                }
            }
        }
        #endregion

        //窗体关闭发生
        private void FrmUserInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            RoleManage.SerializeRole();
        }
    }
}
